﻿#include "dxstdafx.h"
#include "CommonStruct.h"

bool _stDate::operator<(const _stDate& ti) const
{
	if (year < ti.year)
	{
		return true;
	}
	else if (year == ti.year)
	{
		if (month < ti.month)
		{
			return true;
		}
		else if (month == ti.month)
		{
			return day < ti.day;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

bool _stDate::SetYMDbyWstr(std::wstring strDate)
{
	if (strDate.empty())
	{
		return false;
	}
	size_t pos1 = strDate.find_first_of(L".");
	if (strDate.npos == pos1)
	{
		return false;
	}
	size_t pos2 = strDate.find_first_of(L".", pos1 + 1);
	if (strDate.npos == pos2)
	{
		return false;
	}
	{
		std::wstring strYear = strDate.substr(0, pos1);
		if (strYear.empty() || !isdigit(strYear[0]))
		{
			return false;
		}
		year = _wtoi(strYear.c_str());
	}
	{
		std::wstring strMonth = strDate.substr(pos1 + 1, pos2 - pos1 - 1);
		if (strMonth.empty() || !isdigit(strMonth[0]))
		{
			return false;
		}
		int tempMonth = _wtoi(strMonth.c_str());
		if (tempMonth <= 0 || tempMonth > 12)
		{
			return false;
		}
		month = tempMonth;
	}
	{
		std::wstring strDay = strDate.substr(pos2 + 1);
		if (strDay.empty() || !isdigit(strDay[0]))
		{
			return false;
		}
		int tempDay = _wtoi(strDay.c_str());
		if (tempDay <= 0 || tempDay > 31)
		{
			return false;
		}
		day = tempDay;
	}
	return true;
}

void _stDate::Standardize()
{
	static const int NormalDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //平年各月份的总天数
	static const int LeepDays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //闰年各月份的总天数

	int TheMonthDays;
	if (IsLeepYear())
	{
		TheMonthDays = LeepDays[month];
	}
	else
	{
		TheMonthDays = NormalDays[month];
	}
	
	if (day <= TheMonthDays)
	{
		return;
	}
	day -= TheMonthDays;
	++month;
	if (month <= 12)
	{
		return;
	}
	month -= 12;
	++year;
}

bool _stDate::IsLeepYear() const
{
	if (year % 400 == 0)                      //能整除400为闰年
	{
		return true;
	}
	else if (year % 4 == 0 && year % 100 != 0)      //能整除4但不能整除100为闰年
	{
		return true;
	}
	else
	{
		return false;
	}
}



/*template <typename _TData>
void _TNode<_TData>::InsertParrentNode(const _TNode<_TData>* tNode, bool bOperateParrent)
{
	CHECK_RETURN(tNode);

	if (!m_ParrentList.insert(tNode).second)
	{
		CString strErr("The tree node's parrent nodes are repeated!");
		CommonFunc::ErrorMessageBox(strErr);
	}
	if (bOperateParrent)
	{
		tNode->InsertChildNode(this, false);
	}
}

template <typename _TData>
void _TNode<_TData>::InsertChildNode(const _TNode<_TData>* tNode, bool bOperateChild)
{
	CHECK_RETURN(tNode);

	if (!m_ChildList.insert(tNode).second)
	{
		CString strErr("The tree node's child nodes are repeated!");
		CommonFunc::ErrorMessageBox(strErr);
	}
	if (bOperateChild)
	{
		tNode->InsertParrentNode(this, false);
	}
}

template <typename _TData>
void _TNode<_TData>::EraseParrentNode(const _TNode<_TData>* tNode, bool bOperateParrent)
{
	m_ParrentList.erase(tNode);
	if (bOperateParrent)
	{
		tNode->EraseChildNode(this, false);
	}
}

template <typename _TData>
void _TNode<_TData>::EraseChildNode(const _TNode<_TData>* tNode, bool bOperateChild)
{
	m_ChildList.erase(tNode);
	if (bOperateChild)
	{
		tNode->EraseParrentNode(this, false);
	}
}

template <typename _TData>
_TreeIterator<_TData>::_TreeIterator(const _TreeIterator& it)
{
	_tree = it._tree;
	_lit = it._lit;
}

template <typename _TData>
void _TreeIterator<_TData>::operator =(const _TreeIterator& it)
{
	_tree = it._tree;
	_lit = it._lit;
}

template <typename _TData>
bool _TreeIterator<_TData>::operator ==(const _TreeIterator& it)
{
	return _tree == it._tree && _lit == it._lit;
}

template <typename _TData>
bool _TreeIterator<_TData>::operator !=(const _TreeIterator& it)
{
	return _tree != it._tree || _lit != it._lit;
}

template <typename _TData>
_TreeIterator<_TData>& _TreeIterator<_TData>::operator ++()
{
	++_lit;
	return *this;
}

template <typename _TData>
_TreeIterator<_TData> _TreeIterator<_TData>::operator ++(int)
{
	_TreeIterator<_TData>(*this);
	++_lit;
	return it;
}

template <typename _TData>
_TData& _TreeIterator<_TData>::operator *() const
{
	return (*_lit)->tData;
}

template <typename _TData>
bool _TreeIterator<_TData>::operator !()
{
	return _lit == _tree->_nodes.end();
}*/